package com.sinovatech.ologger.actionlog.vo;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sinovatech.ologger.actionlog.converters.UploadFileFormatter;
import com.sinovatech.ologger.actionlog.converters.UploadFileLinkFormatter;
import com.sinovatech.ologger.actionlog.core.LogHelper;
import com.sinovatech.ologger.actionlog.entity.OperationLog;
import com.sinovatech.ologger.util.DateUtil;
import com.sinovatech.ologger.util.StringHelper;

public class OperationLogVO {

	private static final Log logger = LogFactory.getLog(OperationLogVO.class);

	private static UploadFileFormatter uploadFileFormatter = new UploadFileLinkFormatter();

	private Serializable id;

	private Serializable userId;
	/**
	 * 用户名
	 */
	private String username;

	/**
	 * 操作名称 如"新增员工"、"日报汇算"、"删除请假单"
	 */
	private String operationName;

	/**
	 * 操作详细信息/条件 详细信息由获取时通过统一格式化输出 推荐格式:以"#"分隔属性名和值,以"&"分隔不同属性,在获取时统一格式
	 * 如:姓名#张三&部门#市场部
	 */
	private String operationDetails;

	/**
	 * 操作对象变量的集合 以JSON格式保存 避免直接操作,可通过opcMap对其控制
	 */
	private String operationConditions;

	/**
	 * 操作条件Map 对应operationConditions 不保存到数据库
	 */
	private Map<String, Object> opcMap = new HashMap<String, Object>();

	/**
	 * 方法参数JSON字符串(记录Ajax方法的所有参数)
	 */
	private String conditionsJSON;

	/**
	 * 操作时间
	 */
	private Timestamp createTime = null;

	/**
	 * 仅用于搜索
	 */
	private String fromTime;
	private String toTime;
	private Integer pageNo = 1;
	private Integer pageSize = 50;

	public OperationLogVO() {
	}

	public OperationLogVO(OperationLog log) {
		super();
		try {
			this.id = log.getId();
			this.userId = log.getUserId();
			this.username = log.getUsername();
			this.operationName = log.getOperationName();
			this.operationDetails = log.getOperationDetails();
			this.createTime = log.getCreateTime();
		} catch (Exception e) {
			logger.error(e);
		}
	}

	public static OperationLogVO valueOf(OperationLog log) {
		return new OperationLogVO(log);
	}

	public Serializable getId() {
		return id;
	}

	public void setId(Serializable id) {
		this.id = id;
	}

	public Serializable getUserId() {
		return userId;
	}

	public void setUserId(Serializable userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Timestamp getCreateTime() {
		return createTime;
	}

	public String getCreateTimeStr() {
		return createTime != null ? DateUtil.dateToString(createTime,
				DateUtil.NORMAL_SQL_DATE_FORMAT) : "";
	}

	public void setCreateTime(Timestamp createTime) {
		this.createTime = createTime;
	}

	public String getOperationName() {
		return operationName;
	}

	public void setOperationName(String operationName) {
		this.operationName = operationName;
	}

	public String getOperationDetails() {
		String result = StringHelper.isEmpty(operationDetails) ? ""
				: this.operationDetails.replace(
						OperationLog.SEPARATOR_KEYVALUE, ":").replace(
						OperationLog.SEPARATOR_PROPERTY, ";");
		return result.indexOf("[") > 0 ? getUploadFileOperationDetails(result)
				: result;
	}

	public String getUploadFileOperationDetails(String operationDetails) {
		String[] fileInfo = LogHelper.getUploadFileInfo(operationDetails);
		if (ArrayUtils.isEmpty(fileInfo) || fileInfo.length < 3) {
			return operationDetails;
		}
		return LogHelper.processUploadFileInfo(operationDetails,
				uploadFileFormatter.format(fileInfo[0], fileInfo[1],
						fileInfo[1]));
	}

	public void setOperationDetails(String operationDetails) {
		this.operationDetails = operationDetails;
	}

	public String getOperationConditions() {
		return operationConditions;
	}

	public void addOperationConditions(String key, Object value) {
		this.opcMap.put(key, value);
	}

	public String getConditionsJSON() {
		return conditionsJSON;
	}

	public void setConditionsJSON(String conditionsJSON) {
		this.conditionsJSON = conditionsJSON;
	}

	public Map<String, Object> getOpcMap() {
		return opcMap;
	}

	public String getFromTime() {
		return fromTime;
	}

	public void setFromTime(String fromTime) {
		this.fromTime = fromTime;
	}

	public String getToTime() {
		return toTime;
	}

	public void setToTime(String toTime) {
		this.toTime = toTime;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

}
